<template>
  <div class="dashboard-container">
    <div class="app-container">
      <head-tool>
        <template slot="hou">
          <el-button @click="addshow('0', 1)" type="primary"
            >添加权限</el-button
          >
        </template>
      </head-tool>

      <!-- 下面的表格 -->
      <!-- 如果表格要显示树形数据，必须加 row-key -->
      <!-- default-expand-all： 默认展开所有子节点 -->
      <!-- tree-props：
              需要绑定一个对象，对象用来指定子节点的属性 
              hasChildren：代表判断是否有子节点的属性
      -->

      <el-table
        :data="tableData"
        style="width: 100%; margin-bottom: 20px"
        row-key="id"
        border
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="name" label="名称"> </el-table-column>
        <el-table-column prop="code" label="标识"> </el-table-column>
        <el-table-column prop="description" label="描述"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              v-if="row.type == 1"
              @click="addshow(row.id, 2)"
              type="text"
              >添加</el-button
            >
            <el-button @click="editshow(row.id)" type="text">编辑</el-button>
            <el-button @click="delshow(row.id)" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog :title="title" :visible.sync="showDialog" @close="canShow">
        <el-form ref="form" :model="formData" :rules="rules" label-width="80px">
          <el-form-item prop="name" label="权限名称">
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
          <el-form-item prop="code" label="权限标识">
            <el-input v-model="formData.code"></el-input>
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description"></el-input>
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            ></el-switch>
          </el-form-item>
          <el-row type="flex" justify="center">
            <el-button size="mini" @click="save" type="primary">确定</el-button>
            <el-button size="mini" @click="canShow">取消</el-button>
          </el-row>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getAllQxList,
  addQx,
  reomveQx,
  getQx,
  xiugaiQx
} from '@/api/quanxian.js'
//递归树状
import { transToTreeData } from '@/utils'
import { title } from '@/settings'
export default {
  data () {
    return {
      tableData: [],
      showDialog: false,
      formData: {},
      rules: {}
    }
  },
  created () {
    this.getQxList()
  },
  methods: {
    //获取所有权限列表
    async getQxList () {
      let res = await getAllQxList()
      console.log(res)
      // 注意：返回的res.data是列表数据，要递归转成树形数据
      this.tableData = transToTreeData(res.data, '0')
    },
    //给添加按钮添加添加点击事件
    addshow (pid, type) {
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
      this.getQxList()
    },
    //编辑事件
    async editshow (id) {
      let res = await getQx(id)
      this.formData = res.data
      this.showDialog = true
    },
    //删除按钮点击事件
    async delshow (id) {
      //发请求
      await reomveQx(id)
      this.$message.success('删除成功')
      this.getQxList()
    },
    async save () {
      if (this.formData.id) {
        //做编辑
        await xiugaiQx(this.formData)
      } else {
        //调用接口提示
        await addQx(this.formData)
      }
      //弹出提示
      this.$message.success(this.title + '成功')
      this.showDialog = false
      this.getQxList()
    },
    canShow () {}
  },
  computed: {
    title () {
      return this.formData.id ? '编辑权限' : '修改权限'
    }
  }
}
</script>

<style></style>
